package com.jinmdz.fmis.dao.dao.provider;

import com.jinmdz.fmis.dao.base.BaseProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;

/**
 * 守灵房间Provider
 *
 * @author LiCongLu
 * @date 2020-02-15 16:52
 */
public class WakeMournHallProvider extends BaseProvider {
    /**
     * 按照类型字典编码值查询礼厅房间
     *
     * @param dataCode           类型编码
     * @param outState           出厅状态
     * @param inState            入厅状态
     * @param appointmentInTime  预约入厅时间
     * @param appointmentOutTime 预约出厅时间
     * @param asWake             是否守灵
     * @param asFarewell         是否告别
     * @return
     * @author LiCongLu
     * @date 2020-02-17 16:57
     */
    public String listWakeMournHallDeadByDataCode(@Param("dataCode") Integer dataCode, @Param("outState") Integer outState, @Param("inState") Integer inState, @Param("appointmentInTime") Date appointmentInTime, @Param("appointmentOutTime") Date appointmentOutTime, @Param("asWake") Integer asWake, @Param("asFarewell") Integer asFarewell) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" mourn_ceremony_hall.id,hall_type_code,hall_code,hall_name,hall_describe,hall_state,as_wake,as_farewell");
                builder.append(" ,mourn_ceremony_hall.disable_state,mourn_ceremony_hall.sort,mourn_ceremony_hall.remark,mourn_ceremony_hall.version");
                builder.append(" ,business_id,appointment_in_time,appointment_out_time,business_code,process_state_code,dead_name,member_name ");

                builder.append(" ,( select mourn_ceremony.appointment_in_time from mourn_ceremony where mourn_ceremony.hall_id = mourn_ceremony_hall.id ");
                builder.append("  and mourn_ceremony.deleted = 0 and mourn_ceremony.process_state_code != #{outState} and mourn_ceremony.appointment_in_time is not null");

                // 添加时间判断
                if (appointmentInTime != null) {
                    builder.append(" and  mourn_ceremony.appointment_in_time > #{appointmentInTime,jdbcType=TIMESTAMP} ");
                } else {
                    builder.append(" and  mourn_ceremony.appointment_in_time > now()");
                }

                builder.append("  order by mourn_ceremony.appointment_in_time limit 1) as next_in_time");

                SELECT(builder.toString());
                FROM(" mourn_ceremony_hall ");

                // 关联占用逝者信息
                StringBuilder businessExt = new StringBuilder();
                businessExt.append(" select mourn_ceremony.id as business_id,appointment_in_time,appointment_out_time,funeral_business.business_code,process_state_code,hall_id,dead_name,member_name");
                businessExt.append(" from mourn_ceremony JOIN funeral_business on funeral_business.business_code = mourn_ceremony.business_code ");
                businessExt.append(" where funeral_business.deleted = 0 and mourn_ceremony.deleted = 0 and process_state_code != #{outState} and appointment_in_time is not null ");

                // 判断条件
                StringBuilder whereExt = new StringBuilder();
                // 原预约出厅时间为空
                whereExt.append(" appointment_out_time is null ");

                // 原流程状态值
                if (validAll(inState)) {
                    whereExt.append(" or process_state_code = #{inState}");
                }

                // 原预约入厅时间小于当前入厅时间，原预约出厅时间大于当前入厅时间
                if (appointmentInTime != null) {
                    whereExt.append(" or ( appointment_in_time <= #{appointmentInTime,jdbcType=TIMESTAMP} and appointment_out_time >= #{appointmentInTime,jdbcType=TIMESTAMP})");
                } else {
                    whereExt.append(" or ( appointment_in_time <= now() and appointment_out_time >= now())");
                }

                // 原预约入厅厅时间小于当前出厅时间，原预约出厅时间大于当前出厅时间
                if (appointmentInTime != null) {
                    whereExt.append(" or ( appointment_in_time <= #{appointmentOutTime,jdbcType=TIMESTAMP} and appointment_out_time >= #{appointmentOutTime,jdbcType=TIMESTAMP})");
                }

                // 追加判断条件
                businessExt.append(" and ( " + whereExt.toString() + " ) ");
                // 去重
                businessExt.append(" GROUP BY hall_id ");

                LEFT_OUTER_JOIN(" (" + businessExt.toString() + ") as business on business.hall_id = mourn_ceremony_hall.id ");

                WHERE(" mourn_ceremony_hall.deleted = 0 ");

                // 是否守灵
                if (valid(asWake)) {
                    WHERE(" mourn_ceremony_hall.as_wake = 1 ");
                }

                // 是否告别
                if (valid(asFarewell)) {
                    WHERE(" mourn_ceremony_hall.as_farewell = 1 ");
                }

                WHERE(" hall_type_code = #{dataCode} ");
                ORDER_BY(" mourn_ceremony_hall.sort ");
            }
        }.toString();
    }

    /**
     * 按照礼厅主键获取逝者信息
     *
     * @param hallIds           礼厅主键集合
     * @param appointmentInTime 预约入厅时间
     * @return
     * @author LiCongLu
     * @date 2020-03-08 20:23
     */
    public String listWakeMournDeadByHallIds(@Param("hallIds") ArrayList<Integer> hallIds, @Param("appointmentInTime") Date appointmentInTime) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" hall_id,mourn_ceremony.id as business_id,appointment_in_time,appointment_out_time ");
                builder.append(" ,funeral_business.business_code,process_state_code,dead_name,member_name ");

                SELECT(builder.toString());
                FROM(" mourn_ceremony ");
                JOIN(" funeral_business on funeral_business.business_code = mourn_ceremony.business_code ");

                WHERE(" funeral_business.deleted = 0 and mourn_ceremony.deleted = 0 and appointment_in_time is not null ");

                // 预约入厅时间以后的信息
                if (appointmentInTime != null) {
                    WHERE(" appointment_in_time > #{appointmentInTime,jdbcType=TIMESTAMP} ");
                } else {
                    WHERE(" appointment_in_time > now()");
                }

                // 礼厅主键
                String idString = StringUtils.arrayToDelimitedString(hallIds.toArray(new Integer[]{}), ",");
                WHERE("  hall_id in (" + idString + ")");
                ORDER_BY(" appointment_in_time ");
            }
        }.toString();
    }
}
